﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace NhanDang
{
    public partial class frmMain : Form
    {
        
        public frmMain()
        {
            InitializeComponent();
            obj = new PatternRecognition(192, 160, 7, 0.1);
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void họcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLearning frm = new frmLearning();
            frm.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        Bitmap bitMap;
        Bitmap bit;
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            Image img = Image.FromFile(openFileDialog1.FileName);
            bitMap = new Bitmap(img);
            pictureBox1.Image = ImageProcessing.ToBinaryLocBits(bitMap, 245);

            pictureBox1.Image = ImageProcessing.ToBinaryLocBits(bitMap, (byte)15);
            bit = ImageProcessing.ResizeImage(ImageProcessing.ToBinaryLocBits(bitMap, (byte)15));
            Bitmap bitshow = ImageProcessing.ArrayToBitMap(ImageProcessing.arrInput(bit, 16, 12, 5), 12, 16);
            double[] arr = ImageProcessing.arrInput(bit, 16, 12, 5);
            panel1.Controls.Clear();
            for (int i = 0; i < bitshow.Height; i++)
            {
                for (int j = 0; j < bitshow.Width; j++)
                {
                    Panel pn = new Panel
                    {
                        Size = new Size(10, 10),
                        Name = "pn" + i + j,
                        Location = new Point(j * 10, i * 10),
                        BackColor = bitshow.GetPixel(j, i)
                    };
                    panel1.Controls.Add(pn);
                }
            }
            obj.ArrDataInput = ImageProcessing.arrInput(bit, 16, 12, 5);
            
        }
        PatternRecognition obj;
        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image==null)
            {
                MessageBox.Show("Bạn chưa chọn hình");
                return;
            }
            double min = double.MaxValue;
            FileInfo f = new FileInfo(Application.StartupPath + "\\Data\\");
            string re = "";
            foreach (FileInfo item in f.Directory.GetFiles())
            {
                double[][,] arrData = DataProvider.ReadData(item.Name.Split('.')[0], 192, 7, 160);
                double[,] arrWInput = arrData[0];
                double[,] arrWOutput = arrData[1];
                double[] ValueOutput = new double[7];
                for (int i = 0; i < 7; i++)
                {
                    ValueOutput[i] = arrData[2][0, i];
                }
                PatternRecognition pat = new PatternRecognition(192, 160, 7, ImageProcessing.arrInput(bit, 16, 12, 5), ValueOutput, arrWInput, arrWOutput, 0.1);
                double ss = pat.FindNumberError();

                if (ss < min)
                {
                    min = ss;
                    re = item.Name;
                }
            }
            MessageBox.Show(DataProvider.Name(re));
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void vềChươngTrìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About frma = new About();
            frma.ShowDialog();
        }
    }
}
